package com.woniu.repaircenter.service.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.annotation.RequirePermission;
import com.woniu.annotation.RequireRole;
import com.woniu.repaircenter.models.dto.HousingMaterialDTO;
import com.woniu.repaircenter.models.form.InsertMaterialForm;
import com.woniu.repaircenter.models.form.SearchMaterialForm;
import com.woniu.repaircenter.models.form.UpdateMaterialForm;
import com.woniu.repaircenter.models.param.SearchMaterialParam;
import com.woniu.repaircenter.models.param.UpdateMaterialParam;
import com.woniu.repaircenter.service.model.HousingMaterial;
import com.woniu.repaircenter.service.service.HousingMaterialService;
import com.woniu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 材料表 前端控制器
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@RestController
@RequestMapping("/housingMaterial")
@Api(tags = "材料管理")
@Validated
public class HousingMaterialController {
    @Autowired
    private HousingMaterialService housingMaterialService;

    @ApiOperation(value = "新增材料", notes = "新增材料")
    @PostMapping("/insertHousingMaterial")
    @RequireRole("管理员")
    @RequirePermission("新增材料")
    public Result insertHousingMaterial(@Valid @NotNull @RequestBody @ApiParam(name = "insertMaterialForm", value = "材料信息", required = true) InsertMaterialForm insertMaterialForm) throws Exception {
        housingMaterialService.insertHousingMaterial(BeanUtil.toBean(insertMaterialForm, HousingMaterial.class));
        return Result.getResult().setMsg("新增材料成功").setCode(200);
    }

    @ApiOperation(value = "更改材料状态为可用", notes = "更改材料状态为可用")
    @DeleteMapping ("/recoverHousingMaterial")
    @RequireRole("管理员")
    @RequirePermission("启用材料")
    public Result recoverHousingMaterial(@NotBlank(message = "材料编号不能为空") @RequestParam @ApiParam(name = "housingMaterialCode", value = "材料编号", required = true) String housingMaterialCode) throws Exception {
        housingMaterialService.recoverMaterial(housingMaterialCode);
        return Result.getResult().setMsg("恢复材料成功").setCode(200);
    }

    @ApiOperation(value = "更改材料状态为不可用", notes = "更改材料状态为不可用")
    @DeleteMapping("/removeHousingMaterial")
    @RequireRole("管理员")
    @RequirePermission("禁用材料")
    public Result removeHousingMaterial(@NotBlank(message = "材料编号不能为空") @RequestParam @ApiParam(name = "housingMaterialCode", value = "材料编号", required = true) String housingMaterialCode) throws Exception {
        housingMaterialService.removeMaterial(housingMaterialCode);
        return Result.getResult().setMsg("删除材料成功").setCode(200);
    }

    @ApiOperation(value = "材料列表查询", notes = "材料列表查询")
    @PostMapping("/searchHousingMaterialList")
    public Result searchHousingMaterialList(@RequestBody @ApiParam(name = "searchMaterialForm", value = "查询条件", required = true) SearchMaterialForm searchMaterialForm) throws Exception {
        PageInfo<HousingMaterialDTO> pageInfo = housingMaterialService.searchHousingMaterialList(BeanUtil.toBean(searchMaterialForm, SearchMaterialParam.class));
        return Result.getResult(PageInfo.class).setMsg("查询材料列表成功").setCode(200).setData(pageInfo);
    }

    @ApiOperation(value = "材料详情修改", notes = "材料详情修改")
    @PostMapping("/updateHousingMaterial")
    @RequireRole("管理员")
    @RequirePermission("修改材料")
    public Result updateHousingMaterial(@Valid @NotNull @RequestBody @ApiParam(name = "updateMaterialForm", value = "修改参数", required = true) UpdateMaterialForm updateMaterialForm) throws Exception {
        housingMaterialService.updateHousingMaterial(BeanUtil.toBean(updateMaterialForm, UpdateMaterialParam.class));
        return Result.getResult().setMsg("修改材料成功").setCode(200);
    }
    @ApiOperation(value = "材料列表", notes = "材料列表")
    @GetMapping("/getHousingMaterialList")
    public Result getHousingMaterialList() throws Exception {
        List<HousingMaterialDTO> list = housingMaterialService.getHousingMaterialList();
        return Result.getResult(List.class).setMsg("查询材料列表成功").setCode(200).setData(list);
    }
}
